package com.example.yichuangyun.mvvm.ui.fragment;

import android.app.Activity;
import android.os.Bundle;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentBusinessRecordBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.model.order.OrderDetailMoreModel;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.ui.activity.order.view.OrderDetailsViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.ParentOrderMoreAdapter;
import com.example.yichuangyun.utils.RequestUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单详情-更多
 */
public class OrderMoreFragment extends BaseFragment<FragmentBusinessRecordBinding, OrderDetailsViewModel> {

    private String id;
    private OrderListDataModel.OrderRecordsModel orderRecordsModel;
    private List<OrderDetailMoreModel> data;
    private ParentOrderMoreAdapter adapter;
    private MyDialog myDialog;
    private String mTelNumber, businessId, clientName;

    @Override
    protected OrderDetailsViewModel bindModel() {
        return getViewModel(this, OrderDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
    }

    @Override
    protected void lazyLoadData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getString(Constant.idKey);
            orderRecordsModel = (OrderListDataModel.OrderRecordsModel) bundle.getSerializable(Constant.modelKey);
        }
        data = new ArrayList<>();
        data.add(new OrderDetailMoreModel(getString(R.string.base_info)));
        data.add(new OrderDetailMoreModel(getString(R.string.file_info_text)));
        data.add(new OrderDetailMoreModel(getString(R.string.income_pay_text)));
        data.add(new OrderDetailMoreModel(getString(R.string.invoice_record_text)));
        adapter = new ParentOrderMoreAdapter(mContext);
        binding.recyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(mContext, R.layout.layout_empty_list2);
        List<OrderListDataModel.OrderRecordsModel> baseInfoData = new ArrayList<>();
        baseInfoData.add(orderRecordsModel);
        data.get(0).setBaseData(baseInfoData);
        viewModel.getOrderDetailsForFileInfo(id);
        viewModel.getOrderDetailsForInvoice(id);
        viewModel.getOrderDetailsForIncomePay(id);
        initMyClick();
    }

    private void initMyClick() {
        viewModel.fileInfoListMutableLiveData.observe(this, fileInfoModels -> {
            if (fileInfoModels != null) {
                data.get(1).setFileInfoList(fileInfoModels);
            }
        });
        viewModel.incomePayListMutableLiveData.observe(this, incomePayModels -> {
            if (incomePayModels != null) {
                data.get(2).setIncomePayList(incomePayModels);
            }
        });
        viewModel.invoiceListMutableLiveData.observe(this, invoiceModels -> {
            if (invoiceModels != null) {
                data.get(3).setInvoiceList(invoiceModels);
            }
        });
        viewModel.moreRequestCountMutableLiveData.observe(this, count -> {
            if (count == 3) {
                adapter.submitList(data);
            }
        });
        adapter.setOnCallListener((phoneNumber, businessId, userName) -> {
            this.mTelNumber = phoneNumber;
            this.businessId = businessId;
            this.clientName = userName;
            RequestUtils.checkCallNum(mContext, phoneNumber, myDialog, 0, true);
        });
    }

    public static OrderMoreFragment newInstance(String id, OrderListDataModel.OrderRecordsModel orderRecordsModel) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.idKey, id);
        bundle.putSerializable(Constant.modelKey, orderRecordsModel);
        OrderMoreFragment fragment = new OrderMoreFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initDialog() {
        myDialog = new MyDialog((Activity) mContext) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(mContext, mTelNumber);
                Constant.pushBusinessId = businessId;
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound(mContext, mTelNumber, businessId, clientName);
            }
        };
    }
}
